package leetcode;

import java.util.*;

public class LeetCodeMain103 {
    public static void main1(String[] args) {

    }
    public static void main2(String[] args) {
        Scanner in=new Scanner(System.in);
        while (in.hasNext()){
            int n=in.nextInt();
            int[] arr=new int[n];
            int[] dp=new int[n];
            dp[n-1]=0;
            for (int i = 0; i <n; i++) {
                arr[i]=in.nextInt();
            }
            for (int i = 0; i < n-1; i++) {
                back(arr,i,n,arr[i]);
                dp[i]=count;
                count=0;
            }
            for (int i = 0; i < n-1; i++) {
                System.out.printf("%d ",dp[i]);
            }
            System.out.println(dp[n-1]);
        }
    }
    public static int count=0;
    public static void back(int arr[],int startIndex,int n,int target){
        if (startIndex>=n){
            return;
        }
        if (arr[startIndex]<target){
            count++;
        }
        back(arr, startIndex+1, n, target);
    }




    static char vis = 'O';// 代表该棋子已经被访问
    static int[][] dr = new int[][]{
            {1, 0}, {-1, 0}, {0, -1}, {0, 1}
    };
    static int N;
    static char[][] a = new char[N][N];

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        String string = sc.next();
        N=string.charAt(0)-'0';
        // 输入
        a = new char[N][N];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                a[i][j] = string.charAt(j);
            }
        }

        // 统计
        int b_ali = 0, w_ali = 0;

        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if (a[i][j] == 'B')
                    b_ali += BFS('B', i, j, a);
                else if (a[i][j] == 'W')
                    w_ali += BFS('W', i, j, a);
            }
        }

        // 进行输赢判断
    }

    static int BFS(char goal, int x, int y, char[][] a) {
        int ali = 0;// 多少活的
        Queue<Integer> queueX = new LinkedList<Integer>();
        Queue<Integer> queueY = new LinkedList<Integer>();
        queueX.offer(x);
        queueY.offer(y);

        while (queueX.size() != 0) {
            int Xx = queueX.poll(), Yy = queueY.poll();

            if (x < 0 || y < 0 || x >= N || y >= N) {// 这个点不合法,或者这个点不是想要的棋子
                continue;
            }

            if (a[x][y] != goal) {
                continue;
            }

            // 走到这，说明是想要访问的棋子。
            for (int[] ints : dr) {
                int tempx = Xx + ints[0], tempy = Yy + ints[1];
                if (tempx >= 0 && tempx < N && tempy >= 0 && tempy < N && a[tempx][tempy] == 'N') {// a[x][y]作为tempNode的周围，是合法的点，并且，这个点是没有棋子
                    ali += 1;//
                }

                queueX.offer(tempx);
                queueY.offer(tempy);
            }
            // 访问过了
            a[Xx][Yy] = vis;

        }


        return ali;

    }


}
